home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 February / EnigmA AMIGA RUN 15 (1997)(G.R. Edizioni)(IT)[!][issue 1997-02][PLANET CD V].iso / enigma / onlypc / java / samples.bin / LoanCalc.java < prev    next >
Text File  |  1996-03-07  |  4KB  |  121 lines

  1. /*
  2.     This class is a basic extension of the Frame class.  It can be used by
  3.     either an applet or application.  To use it, create a reference to the class,
  4.     instantiate an object of the class, and call the show() method.
  5.  
  6.     example:
  7.  
  8.     LoanCalc myLoanCalc;
  9.     myLoanCalc = new LoanCalc();
  10.     myLoanCalc.show();
  11.  
  12.     You can add controls or menus to LoanCalc with Cafe Studio.
  13.  */
  14.  
  15. import java.awt.*;
  16. import java.lang.*;
  17.  
  18. public class LoanCalc extends Frame {
  19.  
  20.     public LoanCalc() {
  21.  
  22.         super("Loan Calculations");
  23.  
  24.         //{{INIT_CONTROLS
  25.         setLayout(null);
  26.         addNotify();
  27.         resize(insets().left + insets().right + 395, insets().top + insets().bottom + 248);
  28.         label1=new Label("Principal =");
  29.         add(label1);
  30.         label1.reshape(insets().left + 15,insets().top + 33,105,16);
  31.         PrincipalField=new TextField(8);
  32.         add(PrincipalField);
  33.         PrincipalField.reshape(insets().left + 192,insets().top + 29,72,20);
  34.         label2=new Label("Interest Rate =");
  35.         add(label2);
  36.         label2.reshape(insets().left + 15,insets().top + 67,117,18);
  37.         InterestRateField=new TextField(8);
  38.         add(InterestRateField);
  39.         InterestRateField.reshape(insets().left + 192,insets().top + 68,72,18);
  40.         label3=new Label("Length of Loan (years) =");
  41.         add(label3);
  42.         label3.reshape(insets().left + 15,insets().top + 106,159,18);
  43.         LengthLoanField=new TextField(8);
  44.         add(LengthLoanField);
  45.         LengthLoanField.reshape(insets().left + 192,insets().top + 106,72,18);
  46.         MonthlyPayField=new TextField(8);
  47.         add(MonthlyPayField);
  48.         MonthlyPayField.reshape(insets().left + 192,insets().top + 145,72,18);
  49.         label4=new Label("Monthly Payment =");
  50.         add(label4);
  51.         label4.reshape(insets().left + 15,insets().top + 145,141,18);
  52.         CalcButton=new Button("Calculate");
  53.         add(CalcButton);
  54.         CalcButton.reshape(insets().left + 288,insets().top + 102,84,25);
  55.         //}}
  56.  
  57.         //{{INIT_MENUS
  58.         //}}
  59.         MonthlyPayField.setBackground(Color.gray);
  60.         PrincipalField.requestFocus();
  61.     }
  62.  
  63.  
  64.     public synchronized void show() {
  65.         move(50, 50);
  66.         super.show();
  67.     }
  68.  
  69.  
  70.     public boolean handleEvent(Event event) {
  71.         if (event.id == Event.ACTION_EVENT && event.target == CalcButton) {
  72.             OnCalculateLoan();
  73.             return true;
  74.         }
  75.         else
  76.  
  77.         if (event.id == Event.WINDOW_DESTROY) {
  78.             hide();
  79.             return true;
  80.         }
  81.         return super.handleEvent(event);
  82.     }
  83.  
  84.  
  85.     //{{DECLARE_CONTROLS
  86.     Label label1;
  87.     TextField PrincipalField;
  88.     Label label2;
  89.     TextField InterestRateField;
  90.     Label label3;
  91.     TextField LengthLoanField;
  92.     TextField MonthlyPayField;
  93.     Label label4;
  94.     Button CalcButton;
  95.     //}}
  96.  
  97.     //{{DECLARE_MENUS
  98.     //}}
  99.  
  100.     public void OnCalculateLoan() {
  101.  
  102.         double Principal, InterestRate, LengthLoan, MonthlyPay, IntDec, NumMonths;
  103.  
  104.         try {
  105.             Principal    = ( Double.valueOf(PrincipalField.getText()   ) ).doubleValue();
  106.             InterestRate = ( Double.valueOf(InterestRateField.getText()) ).doubleValue();
  107.             LengthLoan   = ( Double.valueOf(LengthLoanField.getText()  ) ).doubleValue();
  108.         } catch (NumberFormatException e) {return;}
  109.  
  110.         if (Principal < 0 || InterestRate < 0 || LengthLoan < 0) return;
  111.  
  112.         IntDec     = InterestRate / (12.0 * 100.0);
  113.         NumMonths  = LengthLoan * 12.0;
  114.         MonthlyPay = Principal * (IntDec / (1.0 - Math.pow((1.0 + IntDec), -NumMonths)));
  115.  
  116.         MonthlyPayField.setText(String.valueOf(MonthlyPay));
  117.     }
  118. }
  119.  
  120.  
  121.